package zx.x.modules.yshapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;
import zx.x.modules.app.entity.UserEntity;
import zx.x.modules.app.service.UserService;
import zx.x.modules.app.utils.SnowFlake;
import zx.x.modules.yshapp.dto.Order;
import zx.x.modules.yshapp.entity.DeliveryAddressEntity;
import zx.x.modules.yshapp.entity.GoodsEntity;
import zx.x.modules.yshapp.entity.MemberEntity;
import zx.x.modules.yshapp.entity.YOrderEntity;
import zx.x.modules.yshapp.service.DeliveryAddressService;
import zx.x.modules.yshapp.service.GoodsService;
import zx.x.modules.yshapp.service.MemberService;
import zx.x.modules.yshapp.service.OrderService;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;


/**
 *
 * @author x
 * @email x
 */
@RestController
@RequestMapping("yshapp/order")
public class OrderAppController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    @Autowired
    private GoodsService goodsService;

    /**
     *
     */
    @RequestMapping("/add")
    @Transactional
    public R list(Order orderVo){
        SnowFlake idWorker = new SnowFlake(0, 0);

        if(orderVo.getType() == 1){//买会员用钱
            MemberEntity memberEntity = memberService.getById(orderVo.getGoodsId());
            if(memberEntity.getSpecialPrice() != null){
                if(orderVo.getPrice().compareTo(memberEntity.getSpecialPrice()) != 0){
                    R.error("订单价钱与会员价钱不一致");
                }
                if(new Date().after(memberEntity.getCouponEndTime())){
                    return R.error("优惠券已过期");
                }

            }else{
                if(orderVo.getPrice().compareTo(memberEntity.getPrice()) != 0){
                    R.error("订单价钱与会员价钱不一致");
                }
            }
            if(memberEntity.getType() == 1){
                orderVo.setGoodsName("普通会员");
            }else{
                orderVo.setGoodsName("超级会员");
            }
            orderVo.setOrderNo("HY"+idWorker.nextId());// 订单编号
            orderVo.setOrderState(0);//待支付
        }else {//买商品用积分
            //商品积分是否一致
            GoodsEntity goodsEntity = goodsService.getById(orderVo.getGoodsId());
            BigDecimal price = new BigDecimal(String.valueOf(goodsEntity.getPrice()));
            if(price.compareTo(orderVo.getPrice()) !=0){
                return R.error("订单积分与商品积分不一致");
            }

            //积分是否够用
            UserEntity user = userService.getById(orderVo.getUserId());
            BigDecimal point = new BigDecimal(String.valueOf(user.getPoint()));
            if( point.compareTo(orderVo.getPrice()) < 0){
                return R.error("积分不足");
            }

            //收货信息
            DeliveryAddressEntity deliveryAddress = deliveryAddressService.getById(orderVo.getDeliveryAddressId());
            orderVo.setDeliveryAddress(deliveryAddress.getDeliveryAddress());
            orderVo.setRecipient(deliveryAddress.getRecipient());
            orderVo.setMobile(deliveryAddress.getMobile());

            //减去用户积分
            Integer userPoint= point.subtract(orderVo.getPrice()).intValue();
            user.setPoint(userPoint);
            userService.updateById(user);

            //补充订单信息
            if(orderVo.getType() == 2){
                orderVo.setOrderNo("FL"+idWorker.nextId());// 订单编号
            }else {//3 生活商品
                orderVo.setOrderNo("SH"+idWorker.nextId());// 订单编号
            }
            orderVo.setGoodsCoverPic(goodsEntity.getCoverPic());
            orderVo.setGoodsName(goodsEntity.getName());
            Date d= new Date();
            orderVo.setPayTime(d);
            orderVo.setOrderState(1);//已支付
            orderVo.setFinishTime(d);
        }
        orderVo.setDeleteFlag(0);
        orderVo.setCjTime(new Date());//订单时间
        orderService.save(orderVo);

        return R.ok().put("order", orderVo);
    }


    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/detail")
    public R detail(String orderNo ){
        YOrderEntity yOrderEntity =orderService.getOne( new QueryWrapper<YOrderEntity>().eq("order_no", orderNo));

        return R.ok().put("order", yOrderEntity);
    }

}
